package gui.listeners;

import gui.panels.media.MediaContainer;

import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import javax.swing.KeyStroke;
import logger.ApplicationLogger;
import conf.PropReaderWriter;

public class KeyShortcutsListener implements  KeyEventDispatcher{

	MediaContainer container;
	public KeyShortcutsListener(MediaContainer mediaPanel) {
		this.container = mediaPanel;
	}

	
	@Override
	public boolean dispatchKeyEvent(KeyEvent e) {
		ApplicationLogger.getLogger().log(Level.INFO, "Dispatching key event");
		
		String combStart = PropReaderWriter.getKeyBindingsProperty("toggle_start");
		String combEnd = PropReaderWriter.getKeyBindingsProperty("toggle_end");
		ApplicationLogger.getLogger().log(Level.INFO, "toggle_end "+ combEnd);
		ApplicationLogger.getLogger().log(Level.INFO, "toggle_start "+ combStart);
		KeyStroke myKeyStroke = KeyStroke.getKeyStrokeForEvent(e);
		
		  KeyStroke toggleStart = KeyStroke.getKeyStroke(combStart);
		  KeyStroke toggleEnd = KeyStroke.getKeyStroke(combEnd);
		  
		  if(myKeyStroke.equals(toggleStart)){
			 
			  if(container.getPlayer()!=null){
				  long inicio = container.getPlayer().getMediaTime().getNanoseconds();
				  ApplicationLogger.getLogger().log(Level.INFO, "Marcando inicio de corte de video en: "+inicio);
				  container.setStartTime(inicio);  
			  }
			  
			  
		  }else if(myKeyStroke.equals(toggleEnd)){
			  
			  if(container.getPlayer()!=null){
				  long fin = container.getPlayer().getMediaTime().getNanoseconds();
				  ApplicationLogger.getLogger().log(Level.INFO, "Marcando fin de corte de video en: "+fin);
				  container.setStopTime(fin);  
			  }
		  }
		return false;
	}

	

}
